package com.example.practical_training_projects.controller;

import com.example.practical_training_projects.entity.user;
import com.example.practical_training_projects.util.FileUtil;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.practical_training_projects.service.userService;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin //允许跨域执行
@RequestMapping("user")
public class userController {
    @Autowired
    private userService service;

    /**
     *  返回所有用户信息：http://localhost:8080/user/list get请求
     *  添加用户信息：http://localhost:8080/user/add post请求
     *  删除用户信息 http://localhost:8080/user/del/{{user_uid}} get请求
     *  修改用户信息 http://localhost:8080/user/update post请求
     *  获取用户uid http://localhost:8080/user/get/{{user_uid}} get请求

     **/
    @GetMapping("list")
    public List<user> getAlluser(){
        return service.getAlluser();
    }
    //分页
    @GetMapping("/page")
    public PageInfo<user> getAlluserpage(Integer pageNum,Integer pageSize){
        PageInfo<user> pageInfo = service.finduserpage(pageNum,pageSize);
        return pageInfo;
    }

    @PostMapping("add")
    public Integer insertUser(@RequestBody user user_uid) {
        //@RequestBody能接收转换的字符串，匹配到StudentInfo studentInfo中
        return service.insertUser(user_uid);

    }
    @GetMapping("/del/{user_uid}")
    public  Integer deleteUser(@PathVariable("user_uid") Integer user_uid){
        return service.deleteUser(user_uid);
    }
    @PostMapping("update")
    public Integer updateUser(@RequestBody user user1){
        return service.updateUser(user1);
    }
    @GetMapping("/query/{name}")
    public List<user> getuserBYName(@PathVariable("name")String user_name){
        return service.getuserBYName(user_name);
    }

    @GetMapping("/get/{user_uid}")
    public user findUseruid(@PathVariable("user_uid") Integer user_uid){
        return service.findUseruid(user_uid);
    }
    @PostMapping ("/upload")
    public String getPicFileName(MultipartFile file){
        String oldFileName = file.getOriginalFilename();
        System.out.println(oldFileName);
        String typeName = oldFileName.substring(oldFileName.lastIndexOf('.'));
        String filePath = FileUtil.getUpLoadFilePath();
        System.out.println(typeName);
        String newFileName = System.currentTimeMillis()+typeName;

        try {
            FileUtil.uploadFile(file.getBytes(),filePath,newFileName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return newFileName;
    }

    @PostMapping ("/uploadmore")
    public List<String> uploadmore(MultipartFile[] files){
        List<String> list = new ArrayList<>();
        for(MultipartFile file : files){
            String oldFileName=file.getOriginalFilename();
            if(oldFileName == null){
                return list;
            }
            String typeName = oldFileName.substring(oldFileName.lastIndexOf('.'));
            if(!typeName.equals(".png")&&!typeName.equals(".jpg")){
                return list;
            }
            String filePath = FileUtil.getUpLoadFilePath();
            String newFileName = System.currentTimeMillis()+"_"+oldFileName;
            try {
                FileUtil.uploadFile(file.getBytes(),filePath,newFileName);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            list.add(newFileName);
        }
        return list;
    }


}
